<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :table-name="tableName"
    :form-items="formItems"
    :form-data="formData"
    :before-save="handleBeforeSave"
    :table-id="tableId"
    @success="handleSuccess"
    @dismiss="beforeClose"
    :submit="false"
    :dismiss="false"
    label-width="140px"
    title="准入审核"
  >
    <el-form-item>
      <el-button @click="handleAccessPass" class="btn-long" type="success" :loading="buttonLoading">
        准入
      </el-button>
      <el-button
        @click="handleAccessReject"
        class="btn-long"
        type="danger"
        :loading="buttonLoading"
      >
        拒绝
      </el-button>
    </el-form-item>
  </c-form-dialog>
</template>

<script>
import { dateFormat } from '@/common/utils/common'
export default {
  props: {
    visible: Boolean,
    formData: {
      type: Object,
      default: () => ({}),
    },
    tableId: String,
  },
  data() {
    return {
      innerVisible: false,
      tableName: 'credit_agency_access',
      formItems: [
        {
          tag: 'input',
          itemAttrs: { label: '机构名称' },
          attrs: { key: 'agency_name', value: '', disabled: true },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '机构评级',
            rules: [{ required: true, message: '请输入机构评级信息', trigger: 'blur' }],
          },
          attrs: { key: 'rating', value: '', placeholder: '请输入机构评级信息' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '审批授权',
            rules: [{ required: true, message: '请输入授权金额', trigger: 'blur' }],
          },
          attrs: { key: 'authorized_amount', value: '' },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                max: 500000,
                fixed: 2,
                onMin: () => this.$message.error('不能小于0'),
              },
            },
          ],
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '合作开始时间',
            rules: [{ required: true, message: '请选择合作开始时间', trigger: 'change' }],
          },
          attrs: { key: 'cooperate_date_start', value: '', placeholder: '请选择合作开始时间' },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '合作结束时间',
            rules: [{ required: true, message: '请选择合作开始时间', trigger: 'change' }],
          },
          attrs: { key: 'cooperate_date_end', value: '', placeholder: '请选择合作开始时间' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '合作金额',
            rules: [{ required: true, message: '请输入合作金额', trigger: 'blur' }],
          },
          attrs: { key: 'credit_amount', value: '' },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                max: 500000,
                fixed: 2,
                onMin: () => this.$message.error('不能小于0'),
              },
            },
          ],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '资本金',
            // rules: [{ required: true, message: '请输入资本金', trigger: 'blur' }]
          },
          attrs: { key: 'margin_amount', value: '' },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                max: 500000,
                fixed: 2,
                onMin: () => this.$message.error('不能小于0'),
              },
            },
          ],
        },
        {
          tag: 'upload',
          itemAttrs: { label: '附件' },
          attrs: { key: 'attachment', value: '' },
        },
        {
          tag: 'input',
          attrs: { key: 'agency_id' },
          ifShow: false,
        },
      ],
      buttonLoading: false,
    }
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  methods: {
    handleBeforeSave(data) {
      const { agency_id: agencyId, agency_name: agencyName, ...info } = data
      info.access_time = dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss')
      info.access_status = 1
      info.service_id = this.clientId
      info.access_agency_id = agencyId
      return info
    },
    async handleAccessPass() {
      this.buttonLoading = true
      await this.$refs.form.submit()
    },
    handleAccessReject() {
      this.$msgbox({
        title: '提示',
        message: `拒绝准入: ${this.formData.agency_name}?`,
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: async (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonText = '操作中...'
            instance.confirmButtonLoading = true
            await this.$api
              .updateData(this.$serviceName, this.tableName, this.tableId, { access_status: 2 })
              .then(() => {
                this.$message.success('已拒绝！')
                this.handleSuccess()
              })
              .catch(err => {
                this.$message.error(`拒绝失败！${err.status}`)
              })
            instance.confirmButtonLoading = false
            instance.confirmButtonText = '确定'
            done()
          } else {
            done()
          }
        },
      }).catch(() => {})
    },
    handleSuccess() {
      this.buttonLoading = false
      this.beforeClose()
      this.$emit('success', true)
    },
    beforeClose() {
      this.innerVisible = false
      this.$emit('update:visible', false)
    },
  },
}
</script>
<style lang="scss" scoped></style>
